﻿package com.saint {
	
	import adobe.utils.CustomActions;
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Shader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ShaderFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import com.video.Webcam;
	import com.video.WebcamData;
	import flash.utils.ByteArray;
	
	[SWF(width = "640", height = "480", frameRate = "30", backgroundColor = "#440000")]
	
	public class Application extends Sprite {
		
		[Embed("../../RedFilter.pbj", mimeType = "application/octet-stream")]
		private var pixelBenderFilter:Class;
		
		// 
		
		private var shader:				Shader;
		private var filter:				ShaderFilter;
		private var webcam:				Webcam;
		private var webcamData:			WebcamData;
		private var camBitmapData:		BitmapData;
		private var filteredBitmapData:	BitmapData;
		private var output:				Bitmap;
		private var filteredActive:		Bitmap;
		private var motionArea:			MotionBounds;
		private var motionPoint:		MotionPoint;
		private var motionCross:		MotionCross;
		private var displayCam:			Boolean;
		private var displayFiltered:	Boolean;
		private var displayArea:		Boolean;
		private var displayDot:			Boolean;
		private var displayCross:		Boolean;
		
		private var cameraW:int = 640;
		private var cameraH:int = 480;
		
		private var searchColour:int = 0xFFFFFF;
		private var mapColour:int = 0xFFFFFF;
		
		public function Application() {
			
			displayCam		=	true;
			displayFiltered	=	true;
			displayArea		=	false;
			displayDot		=	false;
			displayCross	=	true;
			
			webcamData = new WebcamData(cameraW, cameraH);
			webcam = new Webcam(webcamData);
			
			if (webcam.webcam) {
				init();
			}else {
				trace(WebcamData.CAMERA_NULL);
			}
		}
		
		private function init() {
			camBitmapData = new BitmapData(cameraW, cameraH, false, 0 );
			filteredBitmapData = new BitmapData(cameraW, cameraH, false, 0 );
			
			shader = new Shader(new pixelBenderFilter() as ByteArray);
			filter = new ShaderFilter(shader);
			
			motionArea = new MotionBounds();
			motionPoint = new MotionPoint();
			motionCross = new MotionCross(stage);
			
			output = new Bitmap(camBitmapData);
			filteredActive = new Bitmap(filteredBitmapData);
			
			output.filters = [filter];
			
			if (displayCam) {
				addChild(webcam);
			}
			if (displayFiltered) {
				addChild(output);
			}
			if (displayArea) {
				addChild(motionArea);
			}
			if (displayDot) {
				addChild(motionPoint);
			}
			if (displayCross) {
				addChild(motionCross);
			}
			
			addEventListener(Event.ENTER_FRAME, doEF, false, 0, true);
		}
		
		private function doEF(e:Event):void {
			webcam.graphics.clear();
			
			filteredBitmapData.dispose();
			filteredBitmapData = new BitmapData(cameraW, cameraH, false, 0 );
			filteredActive = new Bitmap(filteredBitmapData);
			
			camBitmapData.draw(webcam);
			filteredBitmapData.draw(output);
			
			if (displayArea) {
				motionArea.update(filteredBitmapData.getColorBoundsRect( mapColour, searchColour, true));
			}
			if (displayDot) {
				motionPoint.update(filteredBitmapData.getColorBoundsRect( mapColour, searchColour, true));
			}
			if (displayCross) {
				motionCross.update(filteredBitmapData.getColorBoundsRect( mapColour, searchColour, true));
			}
		}
	}
	
}